package com.menghao.sso.server.controller.advice;

import com.menghao.sso.server.exception.ValidateFailException;
import com.menghao.sso.server.util.CommonUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * <p>异常统一处理类.<br>
 *
 * @author menghao.
 * @version 2017/11/20.
 */
@ControllerAdvice
public class ControllerEnhance {

    @ExceptionHandler(ValidateFailException.class)
    public ModelAndView validateException(ValidateFailException e) throws UnsupportedEncodingException {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg", URLEncoder.encode(e.getMsg(), "UTF-8"));
        modelAndView.addObject(CommonUtils.SERVICE, e.getService());
        modelAndView.setViewName("redirect:" + CommonUtils.LOGIN_URL);
        return modelAndView;
    }

}
